package com.fzy.core.tags;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.fzy.core.utils.NumberUtils;
import com.fzy.core.utils.StringUtils;
import com.opensymphony.xwork.ActionContext;

public class ExtButtonTag extends TagSupport {
	private static final long serialVersionUID = -98813408830654336L;
	String name;
	String width;
	String onclick;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getWidth() {
		return width;
	}
	public void setWidth(String width) {
		this.width = width;
	}
	public String getOnclick() {
		return onclick;
	}
	public void setOnclick(String onclick) {
		this.onclick = onclick;
	}
	public int doEndTag() throws JspException{
		String userAgent = ((HttpServletRequest)pageContext.getRequest()).getHeader("User-Agent");
		boolean isIE = userAgent.indexOf("MSIE") > -1;
		
		int widthNum = 50;
		if(!StringUtils.isBlank(width)){
			if(width.endsWith("px")){
				width = StringUtils.removeEnd(width, "px");
			}
			width = StringUtils.trim(width);
			widthNum = NumberUtils.toInt(width, widthNum);
		}
		
		String onclickHtml = "";
		if(!StringUtils.isBlank(onclick)){
            if (onclick.startsWith("%{") && onclick.endsWith("}")) {
            	onclick = onclick.substring(2, onclick.length() - 1);
				onclick = (String)ActionContext.getContext().getValueStack().findValue(onclick);
			}
			onclickHtml = " onclick=\""+onclick+"\"";
		}
		
		String html = "";
		
		if(isIE){
			html += "<div" + onclickHtml;
			html += "	style=\"background-image: none; border-right-width: 1px; width: "+widthNum+"px; border-top-width: 1px; border-bottom-width: 1px; border-left-width: 1px\"";
			html += "	class=\"x-btn floater x-btn-default-small x-noicon x-btn-noicon x-btn-default-small-noicon\">";
			html += "	<table>";
			html += "		<tbody>";
			html += "			<tr>";
			html += "				<td";
			html += "					style=\"padding-left: 3px; background-position: 0px -6px\"";
			html += "					class=\"x-frame-tl x-btn-tl x-btn-default-small-tl x-btn-default-small-noicon-tl x-btn-default-small-over-tl x-btn-default-small-focus-tl\"></td>";
			html += "				<td style=\"background-position: 0px 0px; height: 3px\"";
			html += "					class=\"x-frame-tc x-btn-tc x-btn-default-small-tc x-btn-default-small-noicon-tc x-btn-default-small-over-tc x-btn-default-small-focus-tc\"></td>";
			html += "				<td";
			html += "					style=\"padding-left: 3px; background-position: right -9px\"";
			html += "					class=\"x-frame-tr x-btn-tr x-btn-default-small-tr x-btn-default-small-noicon-tr x-btn-default-small-over-tr x-btn-default-small-focus-tr\"></td>";
			html += "			</tr>";
			html += "			<tr>";
			html += "				<td style=\"padding-left: 3px; background-position: 0px 0px\"";
			html += "					class=\"x-frame-ml x-btn-ml x-btn-default-small-ml x-btn-default-small-noicon-ml x-btn-default-small-over-ml x-btn-default-small-focus-ml\"></td>";
			html += "				<td style=\"width: 32px; background-position: 0px 0px\"";
			html += "					class=\"x-frame-mc x-btn-mc x-btn-default-small-mc x-btn-default-small-noicon-mc x-btn-default-small-over-mc x-btn-default-small-focus-mc\"><em><button";
			html += "							hidefocus style=\"width: "+(widthNum - 6)+"px; height: 16px\"";
			html += "							class=\"x-btn-center\" type=\"button\">";
			html += "							<span style=\"width: "+(widthNum - 6)+"px\" class=\"x-btn-inner\">"+name+"</span><span";
			html += "								class=\"x-btn-icon\"></span>";
			html += "						</button> </em></td>";
			html += "				<td";
			html += "					style=\"padding-left: 3px; background-position: right 0px\"";
			html += "					class=\"x-frame-mr x-btn-mr x-btn-default-small-mr x-btn-default-small-noicon-mr x-btn-default-small-over-mr x-btn-default-small-focus-mr\"></td>";
			html += "			</tr>";
			html += "			<tr>";
			html += "				<td";
			html += "					style=\"padding-left: 3px; background-position: 0px -12px\"";
			html += "					class=\"x-frame-bl x-btn-bl x-btn-default-small-bl x-btn-default-small-noicon-bl x-btn-default-small-over-bl x-btn-default-small-focus-bl\"></td>";
			html += "				<td style=\"background-position: 0px -3px; height: 3px\"";
			html += "					class=\"x-frame-bc x-btn-bc x-btn-default-small-bc x-btn-default-small-noicon-bc x-btn-default-small-over-bc x-btn-default-small-focus-bc\"></td>";
			html += "				<td";
			html += "					style=\"padding-left: 3px; background-position: right -15px\"";
			html += "					class=\"x-frame-br x-btn-br x-btn-default-small-br x-btn-default-small-noicon-br x-btn-default-small-over-br x-btn-default-small-focus-br\"></td>";
			html += "			</tr>";
			html += "		</tbody>";
			html += "	</table>";
			html += "</div>";
		}else {
			html += "<div style=\"border-width: 1px; width: "+widthNum+"px;\"" + onclickHtml;
			html += "	class=\"x-btn floater x-btn-default-small x-noicon x-btn-noicon x-btn-default-small-noicon\">";
			html += "	<em><button autocomplete=\"off\"";
			html += "			hidefocus=\"true\" class=\"x-btn-center\"";
			html += "			type=\"button\" style=\"height: 16px;\">";
			html += "			<span style=\"\" class=\"x-btn-inner\"";
			html += "				>"+name+"</span><span";
			html += "				class=\"x-btn-icon\"></span>";
			html += "		</button>";
			html += "	</em>";
			html += "</div>";
		}
		try {
			pageContext.getOut().print(html);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doEndTag();
	}
}
